@echo off
chcp 65001
echo ========================================
echo 退费审核系统 - Nginx 部署脚本
echo ========================================
echo.

:: 检查是否以管理员身份运行
net session >nul 2>&1
if %errorLevel% == 0 (
    echo [√] 管理员权限检查通过
) else (
    echo [×] 请以管理员身份运行此脚本
    pause
    exit /b 1
)

:: 设置 Nginx 安装目录
set NGINX_DIR=D:\Server\nginx-1.28.0
set NGINX_CONF=%NGINX_DIR%\conf\nginx.conf
set PROJECT_DIR=%~dp0

echo [1] 检查 Nginx 安装状态...
if exist "%NGINX_DIR%\nginx.exe" (
    echo [√] Nginx 已安装在 %NGINX_DIR%
) else (
    echo [×] Nginx 未安装，请先下载并解压到 %NGINX_DIR%
    echo     下载地址: http://nginx.org/en/download.html
    echo     选择 Windows 版本，解压到 C:\nginx
    pause
    exit /b 1
)

echo.
echo [2] 备份原始配置文件...
if exist "%NGINX_CONF%.backup" (
    echo [√] 配置文件备份已存在
) else (
    copy "%NGINX_CONF%" "%NGINX_CONF%.backup" >nul
    echo [√] 原始配置文件已备份
)

echo.
echo [3] 创建新的 Nginx 配置...
echo # Nginx 主配置文件 > "%NGINX_CONF%"
echo # 退费审核系统配置 >> "%NGINX_CONF%"
echo. >> "%NGINX_CONF%"
echo worker_processes 1; >> "%NGINX_CONF%"
echo. >> "%NGINX_CONF%"
echo events { >> "%NGINX_CONF%"
echo     worker_connections 1024; >> "%NGINX_CONF%"
echo } >> "%NGINX_CONF%"
echo. >> "%NGINX_CONF%"
echo http { >> "%NGINX_CONF%"
echo     include       mime.types; >> "%NGINX_CONF%"
echo     default_type  application/octet-stream; >> "%NGINX_CONF%"
echo     sendfile        on; >> "%NGINX_CONF%"
echo     keepalive_timeout  65; >> "%NGINX_CONF%"
echo. >> "%NGINX_CONF%"
echo     # 退费审核系统服务器配置 >> "%NGINX_CONF%"
echo     server { >> "%NGINX_CONF%"
echo         listen 8089; >> "%NGINX_CONF%"
echo         server_name localhost; >> "%NGINX_CONF%"
echo. >> "%NGINX_CONF%"
echo         # 前端静态文件根目录 >> "%NGINX_CONF%"
echo         root "%PROJECT_DIR:\=/%"; >> "%NGINX_CONF%"
echo         index index.html; >> "%NGINX_CONF%"
echo. >> "%NGINX_CONF%"
echo         # 静态文件缓存配置 >> "%NGINX_CONF%"
echo         location ~* \.(js^|css^|png^|jpg^|jpeg^|gif^|ico^|svg)$ { >> "%NGINX_CONF%"
echo             expires 1y; >> "%NGINX_CONF%"
echo             add_header Cache-Control "public, immutable"; >> "%NGINX_CONF%"
echo         } >> "%NGINX_CONF%"
echo. >> "%NGINX_CONF%"
echo         # API 代理配置 >> "%NGINX_CONF%"
echo         location /api/ { >> "%NGINX_CONF%"
echo             proxy_pass http://10.1.200.169:6724/api/; >> "%NGINX_CONF%"
echo             proxy_set_header Host $host; >> "%NGINX_CONF%"
echo             proxy_set_header X-Real-IP $remote_addr; >> "%NGINX_CONF%"
echo             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; >> "%NGINX_CONF%"
echo             proxy_set_header X-Forwarded-Proto $scheme; >> "%NGINX_CONF%"
echo. >> "%NGINX_CONF%"
echo             # CORS 头部 >> "%NGINX_CONF%"
echo             add_header Access-Control-Allow-Origin *; >> "%NGINX_CONF%"
echo             add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"; >> "%NGINX_CONF%"
echo             add_header Access-Control-Allow-Headers "Content-Type, Authorization"; >> "%NGINX_CONF%"
echo. >> "%NGINX_CONF%"
echo             # 处理 OPTIONS 预检请求 >> "%NGINX_CONF%"
echo             if ($request_method = 'OPTIONS'^) { >> "%NGINX_CONF%"
echo                 add_header Access-Control-Allow-Origin *; >> "%NGINX_CONF%"
echo                 add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"; >> "%NGINX_CONF%"
echo                 add_header Access-Control-Allow-Headers "Content-Type, Authorization"; >> "%NGINX_CONF%"
echo                 add_header Content-Length 0; >> "%NGINX_CONF%"
echo                 add_header Content-Type text/plain; >> "%NGINX_CONF%"
echo                 return 204; >> "%NGINX_CONF%"
echo             } >> "%NGINX_CONF%"
echo         } >> "%NGINX_CONF%"
echo. >> "%NGINX_CONF%"
echo         # 前端路由配置 >> "%NGINX_CONF%"
echo         location / { >> "%NGINX_CONF%"
echo             try_files $uri $uri/ /index.html; >> "%NGINX_CONF%"
echo         } >> "%NGINX_CONF%"
echo. >> "%NGINX_CONF%"
echo         # 错误页面 >> "%NGINX_CONF%"
echo         error_page 404 /index.html; >> "%NGINX_CONF%"
echo     } >> "%NGINX_CONF%"
echo } >> "%NGINX_CONF%"

echo [√] Nginx 配置文件已创建

echo.
echo [4] 停止现有 Nginx 进程...
taskkill /f /im nginx.exe >nul 2>&1
echo [√] 已停止现有 Nginx 进程

echo.
echo [5] 测试 Nginx 配置...
cd /d "%NGINX_DIR%"
nginx.exe -t
if %errorLevel% == 0 (
    echo [√] Nginx 配置测试通过
) else (
    echo [×] Nginx 配置测试失败
    pause
    exit /b 1
)

echo.
echo [6] 启动 Nginx 服务...
start "" nginx.exe
timeout /t 2 >nul

:: 检查 Nginx 是否启动成功
tasklist /fi "imagename eq nginx.exe" 2>nul | find /i "nginx.exe" >nul
if %errorLevel% == 0 (
    echo [√] Nginx 启动成功
    echo.
    echo ========================================
    echo 部署完成！
    echo ========================================
    echo 访问地址: http://localhost:8089
    echo 前端文件: %PROJECT_DIR%
    echo API代理: http://10.1.200.169:6724/api/
    echo ========================================
    echo.
    echo 管理命令:
    echo   停止服务: taskkill /f /im nginx.exe
    echo   重启服务: nginx.exe -s reload
    echo   查看日志: type "%NGINX_DIR%\logs\error.log"
    echo.
) else (
    echo [×] Nginx 启动失败
    echo 请检查错误日志: %NGINX_DIR%\logs\error.log
)

echo 按任意键退出...
pause >nul